import {createRouter, createWebHistory} from "vue-router";
import Home from "../views/Home.vue";
// import store from '../store'

// 页面刷新时，重新赋值token
// if (sessionStorage.getItem('token')) {
//     store.commit('set_token', sessionStorage.getItem('token'))
// }

const routes = [
    {
        path: '/',
        redirect: '/login'
    }, {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: "/dashboard",
                name: "dashboard",
                meta: {
                    title: '我的桌面'
                },
                component: () => import ("../views/Dashboard/Dashboard.vue")
            }, {
                path: "/editserve",
                name: "editserve",
                meta: {
                    title: '服务申请模板修改'
                },
                component: () => import ("../views/Edit/EditServe")
            }, {
                path: "/editsuggestion",
                name: "editsuggestion",
                meta: {
                    title: '意见回复模板修改'
                },
                component: () => import ("../views/Edit/EditSuggestion.vue")
            }, {
                path: "/server",
                name: "server",
                meta: {
                    title: '服务申请工单回复模板'
                },
                component: () => import ("../views/ServerApply/Server.vue")
            },{
                path: "/reply",
                name: "reply",
                meta: {
                    title: '意见工单回复模板'
                },
                component: () => import ("../views/SuggestionReply/Suggestion.vue")
            }, {
                path: "/permission",
                name: "permission",
                meta: {
                    title: '权限管理',
                    permission: true
                },
                component: () => import ("../views/Permission/Permission.vue")
            }, {
                path: "/userManage",
                name: "userManage",
                meta: {
                    title: '用户管理',
                    permission: true
                },
                component: () => import ("../views/Permission/UserManage.vue")
            }, {
                path: "/departManage",
                name: "departManage",
                meta: {
                    title: '部门管理',
                    permission: true
                },
                component: () => import ("../views/Permission/DepartManage.vue")
            }, {
                path: '/404',
                name: '404',
                meta: {
                    title: '找不到页面'
                },
                component: () => import ('../views/Permission/404.vue')
            }, {
                path: '/403',
                name: '403',
                meta: {
                    title: '没有权限'
                },
                component: () => import ('../views/Permission/403.vue')
            }
        ]
    }, {
        path: "/login",
        name: "Login",
        meta: {
            title: '登录'
        },
        component: () => import ("../views/Login/Login.vue")
    }
];

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | 萧山工单系统`;
    // 目前模拟账号只有两个：00000001（管理员账号）；0000002（普通用户）
    const role = localStorage.getItem('ms_username')==='0000001'? 'admin' : 'user';
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permission) {
        // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
        role === 'admin' ? next() : next('/403');
    } else {
        next();
    }
});

export default router;
